﻿using Nortec.Security.DataObjects;
using Nortec.Security.BusinessEntities;
using System.Collections;
using System.Text;
using System;


namespace Nortec.Security.BusinessComponents
{
    public class BCSecurity
    {
        #region Methods: Public
        public static string Desencripta(string texto)
        {
            return new string(Encoding.UTF8.GetChars(Convert.FromBase64String(texto)));
        }

        public static string Encripta(string texto)
        {
            byte[] byteValue = Encoding.UTF8.GetBytes(texto);
            return Convert.ToBase64String(byteValue);
        }
        public void AddUserToRole(string username, string roleName, string applicationName, string companyName)
        {
            new DOSecurity().AddUserToRole(username, roleName, applicationName, companyName);
        }
        public BEEmpresa.EmpresaDataTable GetCompanies(string applicationName)
        {
            return new DOSecurity().GetCompanies(applicationName);
        }
        public BESucursal.SucursalDataTable GetBranches(int companyId)
        {
            return new DOSecurity().GetBranches(companyId);
        }
        
        public int GetApplicationId(string applicationName, int companyId)
        {
            return new DOSecurity().GetApplicationId(applicationName, companyId);
        }
        public int CreateRole(string roleName, string roleDescription, int applicationId)
        {
            return new DOSecurity().CreateRole(roleName, roleDescription, applicationId);
        }
        public int CreateRole(string roleName, string roleDescription, int applicationId, ArrayList rights)
        {
            return new DOSecurity().CreateRole(roleName, roleDescription, applicationId, rights);
        }
        public bool UpdateRole(string roleName, string roleDescription, int roleId, ArrayList rights)
        {
            return new DOSecurity().UpdateRole(roleName, roleDescription, roleId, rights);
        }
        public bool DeleteRole(string roleName, int applicationId, bool throwOnPopulatedRole)
        {
            return new DOSecurity().DeleteRole(roleName, applicationId, throwOnPopulatedRole);
        }
        public BEUsuario.UsuarioDataTable FindUsersInRole(string roleName, string usernameToMatch, int applicationId)
        {
            return new DOSecurity().FindUsersInRole(roleName, usernameToMatch, applicationId);
        }
        public BERol.RolDataTable GetAllRoles(int applicationId)
        {
            return new DOSecurity().GetAllRoles(applicationId);
        }
        public BEUsuario.UsuarioDataTable GetAllUsers(int applicationId)
        {
            return new DOSecurity().GetAllUsers(applicationId);
        }
        public BERol.RolDataTable GetRolesForUser(string username, int applicationId)
        {
            return new DOSecurity().GetRolesForUser(username, applicationId);
        }
        public BEUsuario.UsuarioDataTable GetUsersInRole(string roleName, int applicationId)
        {
            return new DOSecurity().GetUsersInRole(roleName, applicationId);
        }
        public bool IsUserInRole(string username, string roleName, int applicationId)
        {
            return new DOSecurity().IsUserInRole(username, roleName, applicationId);
        }
        public bool RemoveUsersFromRoles(string[] usernames, string[] roleNames, int applicationId)
        {
            return new DOSecurity().RemoveUsersFromRoles(usernames, roleNames, applicationId);
        }
        public BERol.RolDataTable GetRole(string roleName, int applicationId)
        {
            return new DOSecurity().GetRole(roleName, applicationId);
        }
        public BEUsuario.UsuarioDataTable GetUserByName(string username, int applicationId)
        {
            return new DOSecurity().GetUserByName(username, applicationId);
        }
        public BEPrivilegio.PrivilegioDataTable GetRightsForUser(string username, int applicationId)
        {
            return new DOSecurity().GetRightsForUser(username, applicationId);
        }
        public BEPrivilegio.PrivilegioDataTable GetRightsForRole(string roleName, int applicationId)
        {
            return new DOSecurity().GetRightsForRole(roleName, applicationId);
        }
        public BEPrivilegio.PrivilegioDataTable GetRights(int applicationId, string type)
        {
            return new DOSecurity().GetRights(applicationId, type);
        }
        public bool DeleteUser(int userId)
        {
            return new DOSecurity().DeleteUser(userId);
        }
        public int CreateUser(string userName, string password, string firstName, string lastName, int applicationId, ArrayList roles)
        {
            return new DOSecurity().CreateUser(userName, Encripta(password), firstName, lastName, applicationId, roles);
        }
        public bool UpdateUser(string userName, string password, string firstName, string lastName, int userId, ArrayList roles)
        {
            return new DOSecurity().UpdateUser(userName, Encripta(password), firstName, lastName, userId, roles);
        }
        #endregion
    }
}
